//
//  ViewByMonthViewController.m
//  One
//
//  Created by mac on 16/5/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewByMonthViewController.h"
#import "OneCollectionViewCell.h"
#import "WaterflowLayout.h"
#import "HPObject.h"
#import "ViewByHpTitleViewController.h"

#define URL_BASE @"http://v3.wufazhuce.com:8000/api/hp/bymonth/"

@interface ViewByMonthViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,WaterflowDelegate>

{
    NSArray *hpObjArray;
    UICollectionView *myCollectionView;
    NSMutableArray  *hArr; //记录每个cell的高度
}

@end

static NSString * const reuseIdentifier = @"cell";

@implementation ViewByMonthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = _monthStr;
    
    hArr = [[NSMutableArray alloc] init];
    hpObjArray = [[NSArray alloc]init];
    WaterflowLayout *flowLayout=[[WaterflowLayout alloc]init];
    flowLayout.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    myCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH ,HEIGHT - 64) collectionViewLayout:flowLayout];
    myCollectionView.dataSource=self;
    myCollectionView.delegate=self;
    myCollectionView.backgroundColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1];
    [myCollectionView registerNib:[UINib nibWithNibName:@"OneCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self.view addSubview:myCollectionView];
    
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",URL_BASE,_monthStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        hpObjArray = [HPObject mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        [myCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return hpObjArray.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HPObject *object = hpObjArray[indexPath.row];
    CGFloat conHeight = [self caculateHeightWithStr:object.hp_content];
    [hArr addObject:[NSNumber numberWithFloat:(conHeight + 20)]];
    
    return  CGSizeMake(0, conHeight + 20);  //设置cell宽高
}

- (CGFloat)getCellHeightAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [hArr[indexPath.row] floatValue];
    
    return height;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HPObject *object = hpObjArray[indexPath.row];
    
    OneCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    [cell.hpImageView sd_setImageWithURL:[NSURL URLWithString:object.hp_img_url]];
    cell.hpTitleLabel.text = object.hp_title;
    cell.hpContentLabel.text = object.hp_content;
    CGFloat fontSize = 0;
    if (WIDTH == 320) {
        fontSize = 12;
    }
    else
    {
        fontSize = 15;
    }
     cell.hpContentLabel.font = cell.hpTitleLabel.font = [UIFont systemFontOfSize:fontSize];
    
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
- (CGFloat)caculateHeightWithStr:(NSString *)str
{
    
    CGFloat fontSize = 0;
    if (WIDTH == 320) {
        fontSize = 12;
    }
    else
    {
        fontSize = 15;
    }
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake([[UIScreen mainScreen]bounds].size.width/2-30, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|
                   NSStringDrawingUsesDeviceMetrics  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
    
    return rect.size.height;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ViewByHpTitleViewController *dayViwe = [[ViewByHpTitleViewController alloc] init];
    dayViwe.hpObj = hpObjArray[indexPath.row];
    [self.navigationController pushViewController:dayViwe animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
